function questionMouseOver(e) {
    var tip = document.getElementsByClassName("tip")[0];
    tip.style.display = "inline";
}
function questionMouseOut(e) {
    var tip = document.getElementsByClassName("tip")[0];
    tip.style.display = "none";
}
function rankInvitationMouseOver(e) {
    e.lastElementChild.style.display = "block";
}
function rankInvitationMouseOut(e) {
    e.lastElementChild.style.display = "none";
}
function mdSwitch() {
    var mdValue = document.getElementById("md-area").value;
    var converter = new showdown.Converter();
    var html = converter.makeHtml(mdValue);
    document.getElementById("show-area").innerHTML = html;
    MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}
function showSwitch(element) {
    var show = document.getElementById("show");
    if (element.id == "eye") {
        element.id = "eye-slash";
        show.style.display = "none";
        element.style.color = "#467a69";
    } else {
        element.id = "eye";
        show.style.display = "block";
        element.style.color = "#00d1b2";
    }
}
//点击主标签issue按钮调出编辑框
function showTag() {
    document.getElementsByClassName("edit-tag")[0].style.display = "block";
}
//点击tag-top按钮
function tagTop() {
    document.getElementsByClassName("edit-tag")[0].style.display = "none";
}
//点击调出文件插入框ok
function openDialog(i) {
    var dialogBorder = document.getElementsByClassName("md-upload")[i];
    dialogBorder.style.display = "block";
}
//点击插入函数
function insert() {
    for (var i = 0; i < 3; i++) {
        if (document.getElementsByClassName("fileAddress")[i].value != "") {
            var fileAddress = document.getElementsByClassName("fileAddress")[i].value;
            var originaltext = document.getElementById("md-area").value;
            if (i == 0) {
                document.getElementById("md-area").value = originaltext + "![](" + fileAddress + ")";
            } else if (i == 1) {
                document.getElementById("md-area").value = originaltext +
                    "<video id='video' controls=' preload='none' poster=''>" +
                    "<source id='mp4' src='" + fileAddress + "' type='video/mp4'></video>"
            } else {
                document.getElementById("md-area").value = originaltext +
                    "<a class='appendix' href='" + fileAddress + "'>" +
                    fileAddress + "</a>";
            }
            cancelUpload();
            mdSwitch();
            return;//退出函数
        }
    }
    alert("文件路径不能为空");
}
//取消上传函数ok
function cancelUpload() {
    for (var i = 0; i < 3; i++) {
        document.getElementsByClassName("md-upload")[i].style.display = "none";
        document.getElementsByName("file")[i].value = "";
        document.getElementsByClassName("fileAddress")[i].value = "";
    }
}
//addFile函数ok
function addFile(i) {
    document.getElementsByName("file")[i].click();
}

function addHeadImage() {
    document.getElementById("headImageInput").click();
}
//展示昵称修改功能
function showUsenameInput() {
    document.getElementsByClassName("user-image-input")[0].style.display = "block";
}
//异步发帖而后刷新网页,防止重复发帖
function issue() {
    var title = document.getElementsByClassName("issue-title")[0].value;
    var content = document.getElementById("md-area").value;
    var imageBegin = content.indexOf("![](") + 4;
    var previewImageUrl = content.substring(imageBegin, imageBegin + content.substring(imageBegin + 1).indexOf(")") + 1);
    if (content != "") {
        //生成详情页的内容部分
        let formData = new FormData();
        formData.append('content', content);
        formData.append('title', title);
        formData.append('imageUrl', previewImageUrl);
        fetch('/issue', {
            method: 'post',
            body: formData,
        }).then(response => response.text()).then(data => {
            if (data == "success") {
                window.location.reload();
            } else {
                alert("HTML生成失败!");
                return;
            }
        }).catch(function (e) {
            alert("error:" + e);
            return;
        })
    } else {
        alert("内容不能为空!");
    }
}

//修改用户昵称
function updateUserName() {
    //获取修改昵称输入框的值
    var username = document.getElementById("updateUsername");
    if (username.value != "") {
        fetch('/updateUsername?username=' + username.value)
            .then(function (res) {
                return res.text();
            })
            .then(function (data) {
                if (data == "修改成功") {
                    document.getElementById("user-name-tag").innerHTML = username.value;
                    username.value = "";
                }
                alert(data);
            }).catch(function (e) {
                alert("error:" + e);
            })
    } else {
        alert("输入的昵称不能为空");
    }
    document.getElementsByClassName("user-image-input")[0].style.display = "none";
}

//readFile函数
function readFile() {
    //这边上传头像
    if (document.getElementById("headImageInput").files.length > 0) {
        var uploadFiles = document.getElementById("headImageInput").files;
        var fileAddress = document.getElementById("userHeadImage");
        let formData = new FormData();
        formData.append('file', uploadFiles[0]);
        fetch('/uploadHeadImage', {
            method: 'post',
            body: formData,
        }).then(response => response.text()).then(data => {
            if (data.substring(0, 8) != "/upload/") {
                alert(data);
                document.getElementById("headImageInput").value = "";
            } else {
                fileAddress.src = data;
            }
        }).catch(function (e) {
            alert("error:" + e);
        })
        return;
    }
    //这边上传编辑框的图片、视频、附件
    for (var i = 0; i < 3; i++) {
        if (document.getElementsByName("file")[i].files.length > 0) {
            var uploadFiles = document.getElementsByName("file")[i].files;
            var fileAddress = document.getElementsByClassName("fileAddress")[i];
            let formData = new FormData();
            formData.append('file', uploadFiles[0]);//这里的file要与后台@RequestParam的参数值相对应
            fetch('/upload', {
                method: 'post',
                body: formData,
            }).then(response => response.text()).then(data => {
                if (data.substring(0, 8) != "/upload/") {
                    alert(data);
                    document.getElementsByName("file")[i].value = "";
                } else {
                    fileAddress.value = data;
                }

            }).catch(function (e) {
                alert("error:" + e);
            })
            return;//退出函数
        }
    }
    alert("本地文件上传失败");
}
window.onload = function () {
    //添加监听器ok
    var uploadImage = document.getElementsByName("file")[0];
    var uploadVideo = document.getElementsByName("file")[1];
    var uploadAppendix = document.getElementsByName("file")[2];
    var uploadHeadImage = document.getElementById("headImageInput");
    uploadImage.addEventListener('change', readFile, false);
    uploadVideo.addEventListener('change', readFile, false);
    uploadAppendix.addEventListener('change', readFile, false);
    if(uploadHeadImage!=null){
        uploadHeadImage.addEventListener('change', readFile, false);
    }
    var times = document.getElementsByClassName("createTime");
    var timesLength = times.length;
    if(timesLength>0){
        for(var i=0;i<timesLength;i++){
            jsDateDiff(times[i]);
        }
    }

}

//刷新,待完善
function updateAll() {

}
//时间显示格式
function jsDateDiff(element){    
    var time = element.innerHTML; 
    var date = time.substring(0,10);
    var d_minutes,d_hours,d_days,d_weeks,d_months,d;
    var timeNow = parseInt(new Date().getTime()/1000);
    pTime_new = new Date(time).getTime()/1000;
    d = timeNow - pTime_new;
    d_months = parseInt(d/60/60/24/30); 
    d_weeks = parseInt(d/60/60/24/7);
    d_days = parseInt(d/60/60/24);       
    d_hours = parseInt(d/60/60);       
    d_minutes = parseInt(d/60);
    if(d_months>0&&d_months<3){
        element.innerHTML = d_months+"个月前"; 
    }else if(d_months<=0&&d_weeks>0){
        element.innerHTML = d_weeks+"周前";
    }else if(d_weeks<=0&&d_days>0){
        element.innerHTML = d_days+"天前";
    }else if(d_days<=0 && d_hours>0){       
        element.innerHTML = d_hours+"小时前";       
    }else if(d_hours<=0 && d_minutes>0){       
        element.innerHTML = d_minutes+"分钟前";       
    }else if(d_months>=3){
        element.innerHTML = date;
    }else{
        element.innerHTML = "刚刚";
    }    
}